package com.atguigu.gmall.list.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.GoodsService;

import com.atguigu.gmall.model.list.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/list")
public class GoodsController {
    @Resource
    private GoodsService goodsService;
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     * 创建索引和映射
     * @return
     */
    @GetMapping("/createIndexAndMapping")
    public Result createIndexAndMapping(){
        elasticsearchRestTemplate.createIndex(Goods.class);
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    /**
     * es 中新增商品
     * @param skuId
     * @return
     */
    @GetMapping("/addGoods/{skuId}")
    public Result addGoods(@PathVariable("skuId") Long skuId){
        goodsService.dbSkuAddIntoEs(skuId);
        return Result.ok();
    }

    /**
     * es 中删除商品
     * @param goodsId
     * @return
     */
    @GetMapping("/removeGoods/{goodsId}")
    public Result removeGoods(@PathVariable("goodsId") Long goodsId){
        goodsService.removeEs(goodsId);
        return Result.ok();
    }

}
